#include "Common.hpp"

class Init
{
public:
    Init()
    {
        umask(0);
        int n = ::mkfifo(gpath, gmode);
        if (n == 0) // 创建管道成功
        {
            std::cout << "创建管道成功" << std::endl;
        }
        else
        {
            std::cout << "创建管道失败" << std::endl;
            exit(MKFIFOERR);
        }
    }
    ~Init()
    {
        int n = ::unlink(gpath);
        if (n == 0)
        {
            std::cout << "删除管道成功" << std::endl;
        }
        else
        {
            std::cout << "删除管道失败" << std::endl;
            exit(UNLINKERR);
        }
    }

private:
};
Init init;

class Server
{
public:
    Server() : _fd(-1)
    {
    }
    bool OpenforRead()
    {
        int n = Openpipe(O_RDONLY);
        if(n > 0)
        {
            _fd = n;
            return true;
        }
        return false;
    }
    int Readpipe(std::string *out)
    {
        char buff[1024];
        ssize_t n = ::read(_fd, buff, sizeof(buff) - 1);
        if(n > 0)
        {
            buff[n] = 0;
            *out = buff;
            return n;            
        }
        else return 0;
    }
    void ClosePipe()
    {
        CloseHelper(_fd);
    }
    ~Server()
    {
    }

private:
    int _fd;
};
